﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
using eAssessment.lists;
using Microsoft.SharePoint.Utilities;
using System.Web;

namespace eAssessment.Webparts.CreateMChoiceTemplate
{
    public partial class CreateMChoiceTemplateUserControl : UserControl
    {
        protected Dictionary<string, bool> _savedRows;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        override
        protected void OnLoad(EventArgs e)
        {
            _savedRows = ViewState["answerRows"] as Dictionary<string, bool>;
            if (_savedRows != null)
            {
                foreach (string key in _savedRows.Keys)
                {
                    TableRow row = new TableRow();
                    TableCell stringCell = new TableCell();
                    TableCell checkCell = new TableCell();
                    CheckBox box = new CheckBox();

                    checkCell.HorizontalAlign = HorizontalAlign.Center;
                    stringCell.Text = key;
                    bool value;
                    _savedRows.TryGetValue(key, out value);
                    box.Checked = value;
                    checkCell.Controls.Add(box);
                    row.Cells.Add(stringCell);
                    row.Cells.Add(checkCell);

                    answerTable.Rows.Add(row);
                }
            }

        }

        protected void onAddAnswerClick(object sender, EventArgs e)
        {
            if (answerTextbox.Text.Length < 1)
            {
                answerErrorLabel.Text = "Sie können keine leeren Antworten hinzufügen";
                answerErrorLabel.Visible = true;
            }
            else
            {
                answerErrorLabel.Visible = false;

                Table t = answerTable;

                TableCell textCell = new TableCell();
                textCell.Text = answerTextbox.Text;

                TableCell checkCell = new TableCell();
                checkCell.Controls.Add(new CheckBox());
                checkCell.HorizontalAlign = HorizontalAlign.Center;

                TableRow newRow = new TableRow();
                newRow.Cells.Add(textCell);
                newRow.Cells.Add(checkCell);

                answerTable.Rows.Add(newRow);

                answerTextbox.Text = "";


                _savedRows = ViewState["answerRows"] as Dictionary<string, bool>;
                if (_savedRows == null)
                {
                    _savedRows = new Dictionary<string, bool>();
                }
                TableRow row = answerTable.Rows[answerTable.Rows.Count - 1];
                TableCell c = row.Cells[1];
                bool check = false;
                foreach (CheckBox cb in c.Controls)
                {
                    check = cb.Checked;
                }
                _savedRows.Add(row.Cells[0].Text, check);

                ViewState["answerRows"] = _savedRows;
            }
        }

        protected void onCreateQuestionClick(object sender, EventArgs e)
        {
            bool error = false;
            if (answerTable.Rows.Count < 2)
            {
                answerErrorLabel.Text = "Sie müssen min. eine Antwort hinzufügen";
                answerErrorLabel.Visible = true;
                error = true;
            }
            if (questionTextBox.Text.Length < 1)
            {
                questionErrorLabel.Visible = true;
                error = true;
            }
            if (!error)
            {
                answerErrorLabel.Visible = false;
                questionErrorLabel.Visible = false;

                ListAccessManager listHelper = new ListAccessManager();
                int questionID = listHelper.AddMultipleChoiceQuestionTemplate(themeTextbox.Text, questionTextBox.Text);

                foreach (TableRow row in answerTable.Rows)
                {
                    if (row != answerTable.Rows[0])
                    {
                        TableCell c = row.Cells[1];
                        bool check = false;
                        foreach (CheckBox cb in c.Controls)
                        {
                            check = cb.Checked;
                        }
                        listHelper.AddMultipleChoiceAnswerTemplate(row.Cells[0].Text, check, questionID);
                    }
                }
                SPUtility.Redirect("/eAssessment", SPRedirectFlags.Default, HttpContext.Current);
            }
        }

    }
}
